package com.kcl.dfss.view;

import com.kcl.dfss.R;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Paint.FontMetrics;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.ImageView;

public class SpeedPanelView extends ImageView {

	private String TAG = "SpeedPanelView";
	private Matrix matrix = null;
	private Paint paint = null;
	private Bitmap srcBgBitmap = null;
	private int x = 0;
	private int y = 0;
	private int digrees = 0;

	private Bitmap srcIndicator = null;
	private Bitmap srcHalfCycle = null;
	private Bitmap srcSpeed = null;
	private Bitmap srcOdometer = null;
	private Bitmap src0 = null;
	private Bitmap src1 = null;
	private Bitmap src2 = null;
	private Bitmap src3 = null;
	private Bitmap src4 = null;
	private Bitmap src5 = null;
	private Bitmap src6 = null;
	private Bitmap src7 = null;
	private Bitmap src8 = null;
	private Bitmap src9 = null;
	private float speed_w = 0;
	private float speed_h = 0;
	private float odometer_w = 0;
	private float odometer_h = 0;
	private Drawable mBgDrawable = null;
	private Drawable mIndicator = null;

	private int d_indicator = 0;
	private float factor = 1.0f;
	private float bottom_factor = 0.0f;
	private int speed = 20;
	private int odometer = 111230;

	public SpeedPanelView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		init();
	}

	public SpeedPanelView(Context context, AttributeSet attr) {
		super(context, attr);
		init();
	}

	private void init() {
		matrix = new Matrix();
		paint = new Paint();
		matrix.reset();
		paint.reset();
		srcBgBitmap = BitmapFactory.decodeResource(getResources(),
				R.drawable.pannel);
		srcIndicator = BitmapFactory.decodeResource(getResources(),
				R.drawable.needle);
		srcHalfCycle = BitmapFactory.decodeResource(getResources(),
				R.drawable.half_cycle);
		srcSpeed = BitmapFactory.decodeResource(getResources(),
				R.drawable.speed);

		srcOdometer = BitmapFactory.decodeResource(getResources(),
				R.drawable.odometer);

		src0 = BitmapFactory.decodeResource(getResources(), R.drawable.src0);
		src1 = BitmapFactory.decodeResource(getResources(), R.drawable.src1);
		src2 = BitmapFactory.decodeResource(getResources(), R.drawable.src2);
		src3 = BitmapFactory.decodeResource(getResources(), R.drawable.src3);
		src4 = BitmapFactory.decodeResource(getResources(), R.drawable.src4);
		src5 = BitmapFactory.decodeResource(getResources(), R.drawable.src5);
		src6 = BitmapFactory.decodeResource(getResources(), R.drawable.src6);
		src7 = BitmapFactory.decodeResource(getResources(), R.drawable.src7);
		src8 = BitmapFactory.decodeResource(getResources(), R.drawable.src8);
		src9 = BitmapFactory.decodeResource(getResources(), R.drawable.src9);

		digrees = -90 + (int) (180 * (speed / 220.0f));
		bottom_factor = 108 / 493.0f;
		Log.d(TAG, " factor  is " + speed / 220.0f);
		Log.d(TAG, " digrees is " + digrees);
	}

	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		x = getMeasuredWidth();
		y = getMeasuredHeight();

		int src_x = srcBgBitmap.getWidth();
		int src_y = srcBgBitmap.getHeight();
		factor = 1.0f;
		if (x == 0 && y != 0) {
			factor = y * 1.0f / src_y;
			x = (int) (factor * src_x);
		} else if (x == 0 && y == 0) {
			x = src_x;
			y = src_y;
		} else {
			// ( y ==0 && x !=0 ) || ( y !=0 && x != 0)
			// x is the standard
			factor = x * 1.0f / src_x;
			y = (int) (factor * src_y);
		}
		matrix.setScale(factor, factor);

		d_indicator = (int) (factor * ((src_y - 110) - srcIndicator.getHeight()));
		Log.d(TAG, "indicator dy is " + d_indicator);
		setMeasuredDimension(x, y);
		// mBgDrawable = getResources().getDrawable(
		// R.drawable.pannel);
		// mIndicator = getResources().getDrawable(R.drawable.needle);

		// tgtBgBitmap = Bitmap.createScaledBitmap(srcBgBitmap, x, y, false);
		// tgtIndicator = Bitmap.createScaledBitmap(srcIndicator, (int) (1.0f *
		// x
		// / src_x * srcIndicator.getWidth()),
		// (int) (1.0f * x / src_x * srcIndicator.getHeight()), false);
		// Log.d(TAG, "mBgDrawable x y are: " + mBgDrawable.getBounds().width()
		// + " " + mBgDrawable.getBounds().height());
		Log.d(TAG, "measured width and height are: " + x + " " + y);
	}

	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		Log.d(TAG, "onDraw  measured width and height after are: " + x + " "
				+ y);
		Log.d(TAG, " onDraw factor is " + factor);

		Log.d(TAG,
				" srcBgBitmap width and height are " + srcBgBitmap.getWidth()
						+ " " + srcBgBitmap.getHeight());
		Log.d(TAG,
				" srcIndicator width and height are " + srcIndicator.getWidth()
						+ " " + srcIndicator.getHeight());
		Log.d(TAG,
				" srcHalfCycle width and height are " + srcHalfCycle.getWidth()
						+ " " + srcHalfCycle.getHeight());
		Log.d(TAG, " srcSpeed width and height are " + srcSpeed.getWidth()
				+ " " + srcSpeed.getHeight());
		Log.d(TAG,
				" srcOdometer width and height are " + srcOdometer.getWidth()
						+ " " + srcOdometer.getHeight());
		Log.d(TAG,
				" src0 width and height are " + src0.getWidth() + " "
						+ src0.getHeight());
		odometer_w = factor * srcOdometer.getWidth();
		odometer_h = factor * srcOdometer.getHeight();
		speed_w = factor * srcSpeed.getWidth();
		speed_h = factor * srcSpeed.getHeight();
		matrix.reset();
		matrix.setScale(factor, factor);
		canvas.drawBitmap(srcBgBitmap, matrix, paint);

		matrix.postTranslate(
				x / 2 - srcIndicator.getWidth() * factor / 2,
				factor
						* (srcBgBitmap.getHeight() * (1.0f - bottom_factor) - srcIndicator
								.getHeight()) - 5);
		Log.d(TAG, "vertical translate is " + srcBgBitmap.getHeight()
				* (1.0f - bottom_factor));
		matrix.postRotate(
				digrees,
				x / 2,
				factor
						* (srcBgBitmap.getHeight() * (1.0f - bottom_factor) - srcIndicator
								.getWidth()));
		canvas.drawBitmap(srcIndicator, matrix, paint);

		matrix.reset();
		matrix.setScale(factor, factor);
		canvas.drawBitmap(srcHalfCycle, matrix, paint);

		float speed_top = factor
				* (srcBgBitmap.getHeight() * (1.0f - bottom_factor) - 1.25f * srcSpeed
						.getHeight());
		matrix.reset();
		matrix.setScale(factor, factor);
		matrix.postTranslate(x / 2 - speed_w / 2.0f - 5, speed_top);

		canvas.drawBitmap(srcSpeed, matrix, paint);

		float odometer_top = factor
				* (srcBgBitmap.getHeight() * (1.0f - bottom_factor) + (srcBgBitmap
						.getHeight() * bottom_factor - srcOdometer.getHeight()) / 2.0f);
		matrix.reset();
		matrix.setScale(factor, factor);
		matrix.postTranslate(x / 2 - odometer_w / 2.0f - 5, odometer_top);
		canvas.drawBitmap(srcOdometer, matrix, paint);

		paint.reset();
		paint.setTextAlign(Paint.Align.CENTER);
		paint.setFlags(Paint.ANTI_ALIAS_FLAG);
		paint.setColor(Color.BLACK);
		paint.setStrokeWidth(1);
		paint.setTextSize(50);
		FontMetrics metrics = paint.getFontMetrics();
		Log.d(TAG, "metrics  asend is " + metrics.ascent);
		canvas.drawText("km", x / 2 + odometer_w / 1.715f, odometer_top
				+ odometer_h / 2.0f - metrics.ascent / 2.0f, paint);

		paint.reset();
		paint.setTextAlign(Paint.Align.CENTER);
		paint.setFlags(Paint.ANTI_ALIAS_FLAG);
		paint.setColor(Color.BLACK);
		paint.setStrokeWidth(1);
		paint.setTextSize(40);
		// canvas.drawText("KM", 100, 100, paint);
		canvas.drawText("km/h", x / 2, speed_top - speed_h / 5.0f, paint);

		matrix.reset();
		matrix.setScale(factor, factor);
		float digit_width = factor * (src0.getWidth() + 2);
		Log.d(TAG, "onDraw  src0 length is " + src0.getWidth());
		Log.d(TAG, "onDraw  speed length is " + srcSpeed.getWidth());
		matrix.postTranslate(x / 2 - speed_w / 2.0f - 5, speed_top);
		canvas.drawBitmap(getNumberImage(speed / 100), matrix, paint);
		// matrix.postTranslate(x / 2 - speed_w / 2.0f - 5 + digit_width,
		// speed_top);
		matrix.postTranslate(digit_width, 0);
		canvas.drawBitmap(getNumberImage(speed % 100 / 10), matrix, paint);
		matrix.postTranslate(digit_width, 0);
		canvas.drawBitmap(getNumberImage(speed % 10), matrix, paint);

		matrix.reset();
		matrix.setScale(factor, factor);
		matrix.postTranslate(x / 2 - odometer_w / 2.0f - 5, odometer_top);
		canvas.drawBitmap(getNumberImage(odometer / 100000), matrix, paint);
		matrix.postTranslate(digit_width, 0);
		canvas.drawBitmap(getNumberImage(odometer % 100000 / 10000), matrix,
				paint);
		matrix.postTranslate(digit_width, 0);
		canvas.drawBitmap(getNumberImage(odometer % 10000 / 1000), matrix,
				paint);
		matrix.postTranslate(digit_width, 0);
		canvas.drawBitmap(getNumberImage(odometer % 1000 / 100), matrix, paint);
		matrix.postTranslate(digit_width, 0);
		canvas.drawBitmap(getNumberImage(odometer % 100 / 10), matrix, paint);
		matrix.postTranslate(digit_width, 0);
		canvas.drawBitmap(getNumberImage(odometer % 10), matrix, paint);
	}

	private Bitmap getNumberImage(int number) {
		Bitmap bmp = null;
		switch (number) {
		case 0:
			bmp = src0;
			break;
		case 1:
			bmp = src1;
			break;
		case 2:
			bmp = src2;
			break;
		case 3:
			bmp = src3;
			break;
		case 4:
			bmp = src4;
			break;
		case 5:
			bmp = src5;
			break;
		case 6:
			bmp = src6;
			break;
		case 7:
			bmp = src7;
			break;
		case 8:
			bmp = src8;
			break;
		case 9:
			bmp = src9;
			break;
		}
		return bmp;
	}

	public void setSpeed(int real_speed, int real_odometer) {
		speed = real_speed;
		odometer = real_odometer;
		float factor = speed / 220.0f;
		setDigrees(-90 + (int) (180 * factor));
		invalidate();
	}

	public void setOdometer(int real_odometer) {

	}

	public int getSpeed() {
		return speed;
	}

	public void setDigrees(int digree) {
		if (digree > -91 && digree < 91) {
			digrees = digree;
		}
	}
}
